package Linked;



public class LinkedStack<T> {

    private Node head;
    private Node temp;
    private int nWuYuChu=0;
    public class Node{
        public T t;
        public Node node;
        public Node next;
        public Node(T t,Node node){
            this.node=node;
            this.t=t;
        }
        public Node(T t){
            this(t,null);
        }
    }
    public LinkedStack(){
        this.head=null;
}

    public void add(T t,int num) {
        if(num<0||num>nWuYuChu){
            throw new IllegalArgumentException("over the limit");
        }
        else if (num == 0){
            this.add(t);
            return;
        }
        else {
            int n=0;
            Node newNode=this.head;
            while (n!=num-1){
                newNode=newNode.next;
                n++;
            }
            Node node=new Node(t);
            node.next=newNode.next;
            newNode.next=node;
        }

    }

    public void add(T t) {
         Node node = new Node(t);
         if(this.head==null) {
             this.head = node;
         }
        this.temp=this.head;
         while (this.temp.next!=null){
             this.temp=this.temp.next;

         }
        this.temp.next=node;
        node.next = this.head;
        this.head = node;

    }

    public void delete(int num) {
        int n=0;
        Node delNode=this.head;
        Node here=this.head;
        here=here.next;
        if (n!=num-2){
            delNode=delNode.next;
            here=here.next;
            n++;
        }
        if (n==0){
            this.head=this.head.next;
        }
        else if (n==nWuYuChu-1){

            Node pre=this.head;
            while(pre.next != null){
                pre = pre.next;
            }
            pre=null;

        }
        else{
            delNode=here.next;



        }



    }



    public void maopaoCompareUp(int nMaru) {
        int a,b;
        for(a=0;a<=nMaru-1;a++)
        {
            Node upNode=this.head;
            Node useNode=upNode;
            for (b=1;b>a&&b<=nMaru-1;b++){
                useNode=useNode.next;
                if(compareWith(upNode.t,useNode.t))
                {
                    delete(a);
                    delete(b);
                    add(useNode.t,a);
                    add(upNode.t,b);
                }

            }
            upNode=upNode.next;
        }

    }
    public boolean compareWith(T upNode,T useNode){
        String a=""+upNode;
        String b=""+useNode;
        if (a.compareTo(b)<0){
            return true;
        }
        else
            return false;


    }

    @Override
    public String toString() {
        StringBuffer sss = new StringBuffer();
               Node cur = this.head;
                while(cur != null){
                    sss.append(cur.t+"  ");
                    cur = cur.next;
                    }
                sss.append("END");
        return sss.toString();
    }

    public void getInt(int n){
        this.nWuYuChu=n;
    }
}
